package com.xltt.watchlauncher3.view.menu;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;

import com.xltt.watchlauncher3.R;
import com.xltt.watchlauncher3.entity.AppInfoEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class AppInfoEnumerator extends AsyncTask<Integer, Integer, Boolean> {

	public static final String TAG = AppInfoEnumerator.class.getName();
	private Context mContext = null;
	private String mCategory = Intent.CATEGORY_LAUNCHER;
	private CompleteCallback mCBComplete = null;
	private List<AppInfoEntity> mAppList = new ArrayList<AppInfoEntity>();
	public static final boolean SUPPORT_US_LANGUAGE = false;

	
	
	public interface CompleteCallback {
		public void onComplete(List<AppInfoEntity> results);
	}

	public AppInfoEnumerator(Context context, String category, CompleteCallback cb) {
		mContext = context;
		mCategory = category;
		mCBComplete = cb;
	}

	@Override
	protected Boolean doInBackground(Integer... arg0) {
		mAppList.clear();
		queryAppInfo(mAppList);
		return Boolean.TRUE;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (mCBComplete != null)
			mCBComplete.onComplete(mAppList);
	}

	public void queryAppInfo(List<AppInfoEntity> results) {
		PackageManager pm = mContext.getPackageManager();
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(mCategory);
		List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent,0);
//		List<String> pkgList = Arrays.asList(mContext.getResources().getStringArray(R.array.white_list_package));
		for(int i = 0; i < resolveInfos.size(); i++){
			ResolveInfo reInfo = resolveInfos.get(i);
			String appLabel = (String) reInfo.loadLabel(pm);
			String pkgName = reInfo.activityInfo.packageName;
//			if(!pkgList.contains(pkgName)){
//				Log.d(TAG," filter app ,appLabel = " + appLabel + " ,pkgName = " + pkgName);
//				continue;
//			}

			if(pkgName.equals("com.xbd.vehicle") || pkgName.equals("com.xltt.vehicleControl")){
				appLabel = mContext.getString(R.string.vehicle_pkg_lable);
			}
			String activityName = reInfo.activityInfo.name;
			Drawable icon = reInfo.loadIcon(pm);
			Intent launchIntent = new Intent();
			launchIntent.setComponent(new ComponentName(pkgName, activityName));
			AppInfoEntity info = new AppInfoEntity();
			info.setActivityName(activityName);
			info.setPkgName(pkgName);
			info.setAppLabel(appLabel);
			info.setAppIcon(icon);
			info.setIntent(launchIntent);
			info.setCheckedBg(getAppCheckedBg(pkgName));
			//info.setUncheckedBg(getAppUncheckedBg(pkgName));
			Log.d(TAG,info.toString());
			mAppList.add(info);
		}
	}

	/**
	 * 选中背景
	 * @param pkg
	 * @return
	 */
	private int getAppCheckedBg(String pkg){
		if(pkg.equals("com.xltt.call")){
			return SUPPORT_US_LANGUAGE ? R.drawable.dial_icon_bg : R.drawable.dial_icon_bg2;
		}
		if(pkg.equals("com.xltt.clock")){
			return SUPPORT_US_LANGUAGE ? R.drawable.alarm_icon_bg : R.drawable.alarm_icon_bg2;
		}
		if(pkg.equals("com.xltt.health")){
			return SUPPORT_US_LANGUAGE ? R.drawable.activity_icon_bg : R.drawable.activity_icon_bg2;
		}
		if(pkg.equals("com.xltt.heartrate")){
			return SUPPORT_US_LANGUAGE ? R.drawable.healthy_icon_bg : R.drawable.healthy_icon_bg2;
		}
		if(pkg.equals("com.xltt.motion")){
			return SUPPORT_US_LANGUAGE ? R.drawable.motion_icon_bg : R.drawable.motion_icon_bg2;
		}
		if(pkg.equals("com.xltt.music")){//todo 音乐无图标,出版人把系统不添加该应用编译就不会显示
			return SUPPORT_US_LANGUAGE ? R.drawable.record_icon_bg : R.drawable.record_icon_bg2;
		}
		if(pkg.equals("com.xltt.recording")){
			return SUPPORT_US_LANGUAGE ? R.drawable.record_icon_bg : R.drawable.record_icon_bg2;
		}
		if(pkg.equals("com.xltt.timer")){
			return SUPPORT_US_LANGUAGE ? R.drawable.timer_icon_bg : R.drawable.timer_icon_bg2;
		}
		if(pkg.equals("com.xltt.stopwatch")){
			return SUPPORT_US_LANGUAGE ? R.drawable.second_icon_bg : R.drawable.second_icon_bg2;
		}
		if(pkg.equals("com.xltt.settings")){
			return SUPPORT_US_LANGUAGE ? R.drawable.settings_icon_bg : R.drawable.settings_icon_bg2;
		}
		if(pkg.equals("com.xbd.vehicle") || pkg.equals("com.xltt.vehicleControl")){
			return SUPPORT_US_LANGUAGE ? R.drawable.vehicle_icon_bg : R.drawable.vehicle_icon_bg2;
		}
		if(pkg.equals("com.xltt.weather")){
			return SUPPORT_US_LANGUAGE ? R.drawable.weather_icon_bg : R.drawable.weather_icon_bg2;
		}
		return R.drawable.weather_icon_bg;
	}

}
